package calculation

import (
	"fmt"
	"github.com/6tail/lunar-go/calendar"
	. "gpt-api/pkg/ziweiyunshi/ziweiUtil"

	"time"
)

type BaZi struct {
	Birthday    time.Time
	YearGan     string //年干
	YearGanNum  int    //年干代号
	YearZhi     string //年支
	YearZhiNum  int    //年支代号
	MonthGan    string //月干
	MonthGanNum int    //月干代号
	MonthZhi    string //月支
	MonthZhiNum int    //月支代号
	DayGan      string //日干
	DayGanNum   int    //日干代号
	DayZhi      string //日支
	DayZhiNum   int    //日支代号
	HourGan     string //时干
	HourGanNum  int    //时干代号
	HourZhi     string //时支
	HourZhiNum  int    //时支代号
	Month       int    //月
	Day         int    //日
	SolarName   string
	LunarName   string
}

func NewBaZi(TimeStamp int64) *BaZi {
	date := time.Unix(TimeStamp, 0)

	//lunar := calendar.NewLunar(date.Year(), int(date.Month()), date.Day(), date.Hour(), 0, 0)
	////lunar := solar.GetLunar()
	solar := calendar.NewSolar(date.Year(), int(date.Month()), date.Day(), date.Hour(), 0, 0)
	lunar := solar.GetLunar()
	month := lunar.GetMonth()
	day := lunar.GetDay()
	baZi := lunar.GetEightChar()
	baZi.GetYun(1)
	return &BaZi{
		Birthday:    date,
		YearGan:     baZi.GetYear()[0:3],
		YearGanNum:  GetGanNum(baZi.GetYear()[0:3]),
		YearZhi:     baZi.GetYear()[3:6],
		YearZhiNum:  GetZhiNum(baZi.GetYear()[3:6]),
		MonthGan:    baZi.GetMonth()[0:3],
		MonthGanNum: GetGanNum(baZi.GetMonth()[0:3]),
		MonthZhi:    baZi.GetMonth()[3:6],
		MonthZhiNum: GetZhiNum(baZi.GetMonth()[3:6]),
		DayGan:      baZi.GetDay()[0:3],
		DayGanNum:   GetGanNum(baZi.GetDay()[0:3]),
		DayZhi:      baZi.GetDay()[3:6],
		DayZhiNum:   GetZhiNum(baZi.GetDay()[3:6]),
		HourGan:     baZi.GetTime()[0:3],
		HourGanNum:  GetGanNum(baZi.GetTime()[0:3]),
		HourZhi:     baZi.GetTime()[3:6],
		HourZhiNum:  GetZhiNum(baZi.GetTime()[3:6]),
		Month:       month,
		Day:         day,
		SolarName:   solar.String(),
		LunarName:   lunar.String(),
	}
}

func (b *BaZi) GetWuXingJu() *WuXingJuInfo {
	var out *WuXingJuInfo

	YF := GetWuXingJuKey(fmt.Sprintf("%d,%d", b.YearGanNum, b.YearZhiNum))
	res1 := GetWuXingJuName(YF)
	var a []string
	for _, v := range WuXingJuName {
		a = append(a, v)
	}

	out = &WuXingJuInfo{
		YName:  res1.Name,
		YIndex: res1.Index,
		MName:  a[1],
		MIndex: 1,
		DName:  a[2],
		DIndex: 2,
		HName:  a[3],
		HIndex: 3,
	}

	return out

}

func (b *BaZi) GetWuXing() *WuXingInfo {

	w := WuXingInfo{
		YearFive:  GetGanWuXing(b.YearGanNum) + GetZhiWuXing(b.YearZhiNum),
		MonthFive: GetGanWuXing(b.MonthGanNum) + GetZhiWuXing(b.MonthZhiNum),
		DayFive:   GetGanWuXing(b.DayGanNum) + GetZhiWuXing(b.DayZhiNum),
		HourFive:  GetGanWuXing(b.HourGanNum) + GetZhiWuXing(b.HourZhiNum),
	}
	return &w

}

func (b *BaZi) GetBenGongWei() *GongWeiInfo {
	var out GongWeiInfo
	//计算本命盘
	//H 时支

	lifePlate := GetBenMingPan(b.Month, b.HourZhiNum) //定位本命盘-宫位：命宫

	var plate []struct {
		Index int
		Name  string
	}

	for k, v := range lifePlate {
		plate = append(plate, struct {
			Index int
			Name  string
		}{
			Index: v,
			Name:  k,
		})
	}

	out = GongWeiInfo{
		LifePlate: lifePlate,
	}
	return &out
}

func (b *BaZi) GetStarPan(f int) *XingPanInfo {
	var out XingPanInfo
	T1 := GetStarNum(b.Day, f)    //定位紫薇星
	T2 := GetTianFuStar(T1)       //定位天府星
	starMap := GetStarPan(T1, T2) //获取星盘
	out = XingPanInfo{
		StarMap: starMap,
	}
	return &out
}

func (b *BaZi) GetLiuNianGong() *LiuNianGongInfo {
	out := GetLiuNianPan()

	return &LiuNianGongInfo{GongWei: out}
}

func (b *BaZi) GetLiuYueGong() *LiuYueGongInfo {
	out := GetLiuYuePan(b.Month, b.HourZhiNum)
	return &LiuYueGongInfo{GongWei: out}
}
